package com.bzyd.mp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bzyd.mp.model.qo.UserQO;
import com.bzyd.mp.model.po.User;
import com.bzyd.mp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Xianyue Dang
 * @since 2021-08-24
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 新增
     */
    @GetMapping("/save")
    public Object save() {
        User user = new User()
                .setName("zhangsan")
                .setNickName("阿牛")
                .setAge(18)
                .setEmail("zhangsan@qq.com")
                .setMobile("111111");
        userService.save(user);
        return user;
    }

    /**
     * 批量新增
     */
    @GetMapping("/batchSave")
    public Object batchSave() {
        List<User> list = new ArrayList<User>() {{
            add(new User().setName("lisi").setNickName("阿狗").setAge(20).setEmail("lisi@qq.com").setMobile("222222"));
            add(new User().setName("wangwu").setNickName("铁柱").setAge(22).setEmail("wangwu@qq.com").setMobile("333333"));
        }};
        userService.saveBatch(list);
        return list;
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Long id) {
        userService.removeById(id);
        return "ok";
    }

    /**
     * 批量删除
     */
    @GetMapping("/batchDelete/{ids}")
    public Object batchDelete(@PathVariable("ids") String ids) {
        log.info("[batchDelete]-ids：{}", ids);
        String[] idList = ids.split(",");
        userService.removeBatchByIds(Arrays.asList(idList));
        return "ok";
    }

    /**
     * 修改
     */
    @GetMapping("/update/{id}")
    public Object update(@PathVariable("id") Long id) {
        User user = new User().setId(id).setAge(1000);
        userService.updateById(user);
        return "ok";
    }

    /**
     * 批量修改
     */
    @GetMapping("/batchUpdate/{ids}")
    public Object batchUpdate(@PathVariable("ids") String ids) {
        log.info("[batchUpdate]-ids：{}", ids);
        String[] idList = ids.split(",");
        List<User> userList = new ArrayList<>();
        User user;
        for (String id : idList) {
            user = new User().setId(Long.parseLong(id)).setAge(2000);
            userList.add(user);
        }
        userService.updateBatchById(userList);
        return "ok";
    }

    /**
     * 根据id查询
     */
    @GetMapping("/query/{id}")
    public Object query(@PathVariable("id") Long id) {
        return userService.getById(id);
    }

    /**
     * eq 就是 equal等于
     * ne就是 not equal不等于
     * gt 就是 greater than大于
     * lt 就是 less than小于
     * ge 就是 greater than or equal 大于等于
     * le 就是 less than or equal 小于等于
     * in 就是 in 包含（数组）
     * isNull 就是 等于null
     * between 就是 在2个条件之间(包括边界值)
     * like 就是 模糊查询
     */

    /**
     * 列表查询
     */
    @GetMapping("/listQuery")
    public Object listQuery(String name, String mobile, Integer age) {
        log.info("[listQuery]-name：{}，mobile：{}，age：{}", name, mobile, age);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), User::getName, name);
        wrapper.eq(StringUtils.isNotBlank(mobile), User::getMobile, mobile);
        wrapper.ge(Objects.nonNull(age), User::getAge, age);
        wrapper.orderByDesc(User::getCreateTime);
        return userService.list(wrapper);
    }

    /**
     * 分页查询1
     */
    @GetMapping("/pageQuery1")
    public Object pageQuery1(UserQO userQO) {
        log.info("[pageQuery1]-userQO：{}", userQO);
        IPage<User> page = userService.pageQuery1(userQO);
        return page;
    }

    /**
     * 分页查询2
     */
    @GetMapping("/pageQuery2")
    public Object pageQuery2(UserQO userQO) {
        log.info("[pageQuery2]-userQO：{}", userQO);
        IPage<User> page = userService.pageQuery2(userQO);
        return page;
    }

}

